package org.neu.ccs.secureim;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class NetworkHelper {
	
	static public int ReadInt(InputStream in) throws IOException
	{
		int one = in.read();
		int two = in.read();
		int three = in.read();
		int four = in.read();
		
		return (four << 24)
        + ((three & 0xFF) << 16)
        + ((two & 0xFF) << 8)
        + (one & 0xFF);
	}

	static public void WriteInt(OutputStream out, int num) throws IOException
	{
		out.write(num);
		out.write(num >> 8);
		out.write(num >> 16);
		out.write(num >> 24);
	}
	
	static public int BytesToInt(byte[] b)
	{
		if( b.length != 4 )
			return 0;
		
		return (b[0] << 24)
        + ((b[1] & 0xFF) << 16)
        + ((b[2] & 0xFF) << 8)
        + (b[3] & 0xFF);
	}
	
	static public byte[] IntToBytes(int i)
	{
		return new byte[] {
                (byte)(i >>> 24),
                (byte)(i >>> 16),
                (byte)(i >>> 8),
                (byte)i};
	}
}
